package com.example.list;

import androidx.appcompat.app.AppCompatActivity;

import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.tabs.TabItem;
import com.google.android.material.tabs.TabLayout;

import java.lang.reflect.Field;
import java.util.ArrayList;

public class TablayoutActivity extends AppCompatActivity implements View.OnClickListener, TabLayout.BaseOnTabSelectedListener {
    private TabLayout mTabLayout;
    private String mTitles[] = {
            "上海", "头条推荐", "生活", "娱乐八卦", "体育",
            "段子", "美食", "电影", "科技", "搞笑",
            "社会", "财经", "时尚", "汽车", "军事",
            "小说", "育儿", "职场", "萌宠", "游戏",
            "健康", "动漫", "互联网"};

    private TabItem mtab0;


    private LinearLayout llTab0;
    private TextView tvTab0;
    private View view0;
    private LinearLayout llTab1;
    private TextView tvTab1;
    private View view1;
    private LinearLayout llTab2;
    private TextView tvTab2;
    private View view2;
    private LinearLayout llTab3;
    private TextView tvTab3;
    private View view3;
    ArrayList<LinearLayout> list_ll = new ArrayList<>();
    ArrayList<TextView> list_tv = new ArrayList<>();
    ArrayList<View> list_view = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tablayout);


        mTabLayout = (TabLayout) findViewById(R.id.tablayout);
        mTabLayout.setSelectedTabIndicatorColor(Color.WHITE);
//        mTabLayout.settabIndicatorFullWidth
        mTabLayout.setTabIndicatorFullWidth(false);


        //TabLayout的基本使用
        for (int i = 0; i < 4; i++) {
            TabLayout.Tab tab = mTabLayout.newTab();

            tab.setTag(i);
            tab.setText(mTitles[i]);
//            mTabLayout.addTab(tab);
        }
        mTabLayout.removeTabAt(0);
//        mTabLayout.post(new Runnable() {
//            @Override
//            public void run() {
//                setIndicator(mTabLayout, 20, 20);
//            }
//        });

        mTabLayout.addOnTabSelectedListener(this);


        llTab0 = (LinearLayout) findViewById(R.id.ll_tab0);
        tvTab0 = (TextView) findViewById(R.id.tv_tab0);
        view0 = (View) findViewById(R.id.view0);
        llTab1 = (LinearLayout) findViewById(R.id.ll_tab1);
        tvTab1 = (TextView) findViewById(R.id.tv_tab1);
        view1 = (View) findViewById(R.id.view1);
        llTab2 = (LinearLayout) findViewById(R.id.ll_tab2);
        tvTab2 = (TextView) findViewById(R.id.tv_tab2);
        view2 = (View) findViewById(R.id.view2);
        llTab3 = (LinearLayout) findViewById(R.id.ll_tab3);
        tvTab3 = (TextView) findViewById(R.id.tv_tab3);
        view3 = (View) findViewById(R.id.view3);


        list_ll.add(llTab0);
        list_ll.add(llTab1);
        list_ll.add(llTab2);
        list_ll.add(llTab3);


        list_tv.add(tvTab0);
        list_tv.add(tvTab1);
        list_tv.add(tvTab2);
        list_tv.add(tvTab3);

        list_view.add(view0);
        list_view.add(view1);
        list_view.add(view2);
        list_view.add(view3);

        for (int i = 0; i < list_ll.size(); i++) {

            list_ll.get(i).setOnClickListener(this);
        }
        dealView(0);

        list_ll.get(0).setVisibility(View.GONE);

        if (list_ll.get(0).getVisibility() == View.VISIBLE) {
            dealView(0);
        } else {
            dealView(1);
        }
    }

    /**
     * @date:2017/11/10/010
     * @author:平sir
     * @desc: 设置tab指示器的宽度
     */
    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("slidingTabIndicator");
//            if (Build.VERSION.SDK_INT > 28) {
//                tabStrip = tabLayout.getDeclaredField("slidingTabIndicator");
//            } else {
//                tabStrip = tabLayout.getDeclaredField("mTabStrip");
//            }

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }

    @Override
    public void onClick(View v) {
        String ss = "";
        int position = -1;
        switch (v.getId()) {
            case R.id.ll_tab0:
                position = 0;
                break;
            case R.id.ll_tab1:
                position = 1;
                break;
            case R.id.ll_tab2:
                position = 2;
                break;
            case R.id.ll_tab3:
                position = 3;
                break;
        }
        dealView(position);
        Toast.makeText(this, "" + position, Toast.LENGTH_SHORT).show();
    }

    public void dealView(int position) {
        for (int i = 0; i < list_view.size(); i++) {
            if (position == i) {
                list_view.get(position).setVisibility(View.VISIBLE);
                list_tv.get(position).setTextSize(16);

            } else {
                list_view.get(i).setVisibility(View.INVISIBLE);
                list_tv.get(i).setTextSize(14);
            }

        }
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        String ss = "";
        ss = tab.getText().toString();
        if (ss.equals("")) {

        } else if (ss.equals("")) {

        } else if (ss.equals("")) {

        } else if (ss.equals("")) {

        }
//        switch (tab.getPosition()) {
//            case 0:
//                ss = "0";
//                break;
//            case 1:
//                ss = "1";
//                break;
//            case 2:
//                ss = "2";
//                break;
//            case 3:
//                ss = "3";
//                break;
//
//        }
        Toast.makeText(this, ss, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}